<template>
	<view class="content">
		
		
		<view class="top">
			<view class="top_left">
				<image src="../static/login_bg.png"></image>
			</view>
			<view class="top_right">
				<view class="text" :class='[type=="1" ? "active" : ""]'>
					登录
					<view class="line" v-if="type == 1"></view>
				</view>
				<view class="text" @click="toPage('register')" :class='[type=="2" ? "active" : ""]'>
					注册
					<view class="line" v-if="type == 2"></view>
				</view>
			</view>
		</view>
		<view class="title_box">
			<view>您好，</view>
			<view> 欢迎来到蜂盈优品</view>
		</view>
		<view class="con">
			<view class="title">
				<view class="text" @click="switch_but(2)" :class='[type_lx=="2" ? "active" : ""]'>
					账号密码登录
				</view>
				<view class="text" @click="switch_but(1)" :class='[type_lx=="1" ? "active" : ""]'>
					验证码登录
				</view>
			</view>
			<view class="list">
				<input type="text" v-model="telphone" placeholder="输入手机号码" />
			</view>
			<view class="list" v-if="type_lx==2">
				<input type="text" v-model="password" placeholder="输入密码" />
			</view>
			<view class="list" v-if="type_lx==1">
				<input type="text" v-model="codeNum" placeholder="输入验证码" />
				<u-button type="success" @click="getCode" :ripple="true" class="code_but">{{tips}}</u-button>
			</view>
			<view class="button_sign" @click="dl_but">登录</view>
			<view class="tips">
				<view class="circular" @click="grant_but()" :class='[type_grant==1 ? "active" : ""]'></view>
				<view class="text">
					登录即代表你已阅读并同意
					<text v-for="(item,index) in agreement_list" @click="go_agreement(item.id)">{{ item.title }}、</text>
				</view>
			</view>
		</view>
		<u-verification-code :keep-running="true" ref="uCode" @change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	var than;
	export default {
		data() {
			return {
				type: 1,
				type_lx: 2,
				type_grant: 1,
				telphone: '',
				password: '',
				codeNum: '',
				tips: '',
				agreement_list:[],
			}
		},
		onLoad(option) {
			than = this;
			this.agreement_fun()
		},
		methods: {
			agreement_fun(){
				than.post("api/user/agreementList", {
					
				}, function(data) {
					than.agreement_list = data
					//console.log(data,"sss")
				})
			},
			codeChange(text) {
				this.tips = text;

			},
			getCode() {
				if (!this.$u.test.mobile(than.telphone)) {
					than.toast("手机号码格式错误")
					return false
				}
				if (this.$refs.uCode.canGetCode) {
					this.$refs.uCode.start();
					than.post("api/user/getSmsCode", {
						telphone: than.telphone
					}, function(data) {})
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},			
			pwdLogin() {
				if (than.type_grant == 0) {
					than.toast("未同意服务协议")
					return false
				}
				if (!than.password) {
					than.toast("未填写密码")
					return false
				}
				than.post("api/user/login", {
					telphone: than.telphone,
					password: than.password,
				}, function(data) {
					uni.setStorage({
						key: 'token',
						data: data.token,
						success: function() {
							than.setUser(data)
						}
					})
				})
			},
			codeLogin() {
				if (than.type_grant == 0) {
					than.toast("未同意服务协议")
					return false
				}
				if (!than.codeNum) {
					than.toast("未填写验证码")
					return false
				}
				than.post("api/user/smsLogin", {
					telphone: than.telphone,
					smscode: than.codeNum,
				}, function(data) {
					uni.setStorage({
						key: 'token',
						data: data.token,
						success: function() {
							than.setUser(data)
						}
					})
				})
			},
			setUser(data) {
				var json = {};
				json.head_img = data.icon;
				json.uid = data.uid;
				json.telphone = data.telphone;
				json.nickname = data.name;
				uni.setStorage({
					key: 'user',
					data: json,
					success: function() {
						uni.switchTab({
							url: "../../home/index"
						})
					}
				})
			},
			dl_but() {
				if (!than.checkPhone(than.telphone)) {
					than.toast("手机号格式错误")
					return false
				}
				if (this.type_lx == 1) {
					than.codeLogin()
				} else {
					than.pwdLogin()
				}
			},
			switch_but(index) {
				this.type_lx = index
			},
			grant_but() {
				if (this.type_grant == 0) {
					this.type_grant = 1
				} else {
					this.type_grant = 0
				}
			},
			go_agreement(id){
				than.toPage('../login1/agreement?id=' + id)
			},
		}
	}
</script>

<style lang="less">
	page {
		background: white;
	}

	.content {
		padding: 50rpx;
	}

	.top {
		display: flex;
		margin-top: 70rpx;
		.top_left{
			flex: 1;
			image{
				width: 74rpx;
				height: 75rpx;
			}
		}
		.top_right{
			display: flex;
			.text {
				font-size: 36rpx;
				font-weight: 500;
				color: #333333;
				font-family: PingFang SC;
			}
			
			.active {
				color: #202020;
				font-weight: bold;
			}
			
			.text:last-child {
				margin-left: 40rpx;
			}
			
			.line {
				width: 60rpx;
				height: 6rpx;
				background: #494949;
				border-radius: 10rpx;
				margin: auto;
				margin-top: 10rpx;
			}
		}

		
	}
    
	.title_box{
		font-size: 48rpx;
		font-weight: bold;
		color: #343236;
		line-height: 60rpx;
		margin-top:100rpx;
	}
	
	.con {
		.title {
			display: flex;
			align-items: center;
			margin-top: 120rpx;

			.text {
				font-size: 26rpx;
				font-weight: bold;
				color: #999999;
			}

			.active {
				color: #333333;
				font-size: 32rpx;
			}

			.text:last-child {
				margin-left: 20rpx;
			}
		}

		.list {
			display: flex;
			align-items: center;
			margin-top: 30rpx;

			input {
				flex: 1;
				height: 100rpx;
				background: #F5F5F6;
				border-radius: 20rpx;
				font-size: 32rpx;
				font-weight: bold;
				color: #5D5D65;
				padding-left: 40rpx;
			}

			.code_but {
				margin-left: 20rpx;
				width: 220rpx;
				height: 100rpx;
				background: #F4C932;
				border-radius: 20rpx;
				font-size: 32rpx;
				font-weight: bold;
				color: #000000;
				line-height: 100rpx;
				text-align: center;
			}
		}

		.button_sign {
			width: 100%;
			margin-top: 100rpx;
			height: 74rpx;
			background: #000000;
			border-radius: 20rpx;
			line-height:74rpx;
			text-align: center;
			color: white;
			font-weight: 500;
		}

		.tips {
			display: flex;
			margin-top: 100rpx;

			.circular {
				width: 24rpx;
				height: 24rpx;
				border: 2rpx solid #C1C1C1;
				border-radius: 50%;
				margin-top: 10rpx;
			}

			.text {
				margin-left: 20rpx;
				flex: 1;
				font-size: 24rpx;
				font-weight: 500;
				color: #9B9CA0;
				line-height: 40rpx;
			}

			text {
				color: #333333;
			}

			.active {
				background-color: #C1C1C1;
			}
		}
	}
</style>
